/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.SalesView.TempData;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author 伯乐
 */
public class Carts {

    //"数量", "ISBN", "标题", "作者", "价格", "出版日期", "出版商"
    ArrayList<Object[]> booklist = new ArrayList<>();
    
    public void cleanCarts(){
        booklist = new ArrayList<>();
    }
    
    public List<Integer> getNumList(){
        List<Integer> numList = new ArrayList<>();
        for(Object[] book:booklist){
            numList.add( Integer.parseInt( String.valueOf(book[0]) ) );
        }
        return numList;
    }
    
    public List<Double> getPriceList(){
        List<Double> priceList = new ArrayList<>();
        for(Object[] book:booklist){
            priceList.add( Double.parseDouble( String.valueOf(book[4]) ) );
        }
        return priceList;
    }
    
    public List<Object[]> getBooklist() {
        return booklist;
    }
    
    public boolean cartsIsEnpty(){
        return booklist.isEmpty();
    }
    
    public boolean addBook(Object[] b, int num){
        if(b == null){
            return false;
        }
        Object[] str = null;
        for(int i = 0; i < booklist.size(); i++){
            str = booklist.get(i);
            if(str[1].equals(b[0])){ //如果书籍已经存在，则增加数量
                str[0] = String.valueOf(Integer.parseInt(String.valueOf(str[0])) + num);
                return true;
            }
        }
        
        if(b.length>0){
            String[] book = new String[b.length+1]; //如果书籍不存在，则添加书籍
            book[0] = String.valueOf(num);
            System.arraycopy(b, 0, book, 1, b.length);
            if(booklist.add(book)){
                return true;
            }
        }
        
        return false; //如果添加失败，不重新计算总价
    }

    public boolean remove(Object isbn, int num){
        Object[] str = null;
        boolean isValid = false;
        for(int i = 0; i < booklist.size(); i++){ //第i个记录
            str = booklist.get(i);
            if(String.valueOf(str[1]).equals(isbn)){
                int a = Integer.parseInt(String.valueOf(str[0]));
                if(a == num){ //删除所有
                    booklist.remove(i);
                    isValid = true;
                    break;
                } else if(num>0 && num<a){ //删除部分
                    str[0] = String.valueOf(a-num);
                    isValid = true;
                    break;
                } else { //输入不合法
                    isValid = false;
                }
            } else { //没有记录
                isValid = false;
            }
            if(isValid){
                break;
            }
        }
        return isValid;
    }
    
    public Vector<List> cartsTableVector() {
        return new Vector(toBookTable(booklist));
    }

    private Vector<Vector<String>> toBookTable(List<Object[]> list) {
        try {
            Vector<Vector<String>> reList = new Vector<Vector<String>>();

            for (int i = 0; i < list.size(); i++) {
                reList.add(toBookStringArray(list.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private Vector<String> toBookStringArray(Object[] book) {
        Vector<String> list = new Vector<String>();
        for (int i = 0; i < book.length; i++) {
            list.add(String.valueOf(book[i]));
        }
        return list;
    }

}
